// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "merchant_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "申请提现商户ID",
    "label": "申请提现商户ID"
  },
  "applyuser_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "defaultValue": {
      "$env": "uid"
    }
  },
  "amount": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "number"
      },
      {
        "minimum": 1
      }
    ],
    "title": "提现金额",
    "label": "提现金额"
  },
  "fee": {
    "rules": [
      {
        "format": "number"
      },
      {
        "minimum": 0
      }
    ],
    "title": "手续费",
    "defaultValue": 0,
    "label": "手续费"
  },
  "actual_amount": {
    "rules": [
      {
        "format": "number"
      },
      {
        "minimum": 0
      }
    ],
    "title": "实际到账金额",
    "label": "实际到账金额"
  },
  "withdraw_type": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "支付宝",
            "value": 0
          }
        ]
      }
    ],
    "title": "提现方式",
    "defaultValue": 0,
    "label": "提现方式"
  },
  "available_balance": {
    "rules": [
      {
        "format": "number"
      },
      {
        "minimum": 0
      }
    ],
    "title": "可用余额",
    "label": "可用余额"
  },
  "frozen_balance": {
    "rules": [
      {
        "format": "number"
      },
      {
        "minimum": 0
      }
    ],
    "title": "冻结余额",
    "label": "冻结余额"
  },
  "remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "备注",
    "label": "备注"
  },
  "reason": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "拒绝原因",
    "label": "拒绝原因"
  },
  "status": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "审核中",
            "value": 0
          },
          {
            "text": "已通过,待打款",
            "value": 1
          },
          {
            "text": "已拒绝",
            "value": 2
          },
          {
            "text": "打款成功",
            "value": 3
          },
          {
            "text": "打款失败",
            "value": 4
          }
        ]
      }
    ],
    "title": "状态",
    "defaultValue": 0,
    "label": "状态"
  },
  "account_info": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "object"
      }
    ],
    "title": "支付宝账户信息",
    "label": "支付宝账户信息"
  },
  "audit_info": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "审核信息",
    "label": "审核信息"
  },
  "payment_info": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "打款信息",
    "label": "打款信息"
  },
  "create_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "申请时间",
    "label": "申请时间"
  }
}

const enumConverter = {
  "withdraw_type_valuetotext": {
    "0": "支付宝"
  },
  "status_valuetotext": {
    "0": "审核中",
    "1": "已通过,待打款",
    "2": "已拒绝",
    "3": "打款成功",
    "4": "打款失败"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
